package com.web.startup;

import com.mongodb.Mongo;
import com.mongodb.MongoCredential;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * Created with IntelliJ IDEA.
 * User: xf
 * Date: 2016/4/23
 * Time: 14:47
 * To change this template use Editor | File and Code Templates.
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.mongo")
public class MongoDBConfig {

    @Bean
    public MongoClientFactoryBean mongo() {
//        MongoCredential credential = MongoCredential.createMongoCRCredential("lxf", "lxfmongo", "lxf123456".toCharArray());
//        MongoCredential[] credentials = {credential};
        MongoClientFactoryBean mongo = new MongoClientFactoryBean();
        mongo.setHost("127.0.0.1");
//        mongo.setCredentials(credentials);
        return mongo;
    }

    @Bean
    public MongoOperations mongoTemplate(Mongo mongo) {
        return new MongoTemplate(mongo, "lxfmongo");
    }


}
